package com.daka.controller;

import com.daka.entity.Member;
import com.daka.entity.MemberOrg;
import com.daka.entity.Organization;
import com.daka.enumerate.StatusCode;
import com.daka.service.MemberOrgService;
import com.daka.service.MemberService;
import com.daka.service.OrganizationService;
import com.daka.tools.DKQConstant;
import com.daka.tools.Page;
import com.daka.vo.request.OrganizationVo;
import com.daka.vo.response.DataResponseVo;
import com.daka.vo.response.OrganizationResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by yangx
 * CreateTime: 2016/9/18 15:16
 */

@Controller
@RequestMapping("/group")
@Slf4j
public class GroupController {

    @Autowired
    private OrganizationService organizationService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private MemberOrgService memberOrgService;

    @ResponseBody
    @RequestMapping(value = "/queryList" , method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryList(@RequestBody OrganizationVo groupVo){
        if(groupVo.getCity()==null ){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        String city = groupVo.getCity();
        String pageNo = groupVo.getPageNo();
        int pageNo1;
        if(pageNo==null || Integer.valueOf(pageNo)<1){
            pageNo1 =1;
        }else{
            pageNo1=Integer.valueOf(pageNo);
        }
        Page<OrganizationResponseVo> page = organizationService.selectByCity(city,pageNo1);

        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),page);
    }

    @ResponseBody
    @RequestMapping(value = "/queryDetail", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> queryDetail(@RequestBody OrganizationVo groupVo){
        if(groupVo.getGroupCode()==null){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        Organization organization = organizationService.selectByGroupCode(groupVo.getGroupCode());
        OrganizationResponseVo response1 = OrganizationResponseVo.buildVO(organization);
        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage(),response1);
    }


    @ResponseBody
    @RequestMapping(value = "/joinGroup", method = RequestMethod.POST,produces = "application/json; charset=UTF-8")
    public DataResponseVo<Object> joinGroup(@RequestBody OrganizationVo vo){
        if(vo.getGroupCode()==null || vo.getMemberID()==null){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(), StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        String groupCode = vo.getGroupCode();
        String memberID = vo.getMemberID();
        //组织验证
        Organization organization = new Organization();
        organization.setGroupCode(groupCode);
        List<Organization> list1 = organizationService.selectByStatement(organization);
        if(list1==null || list1.size()<1){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        //志愿者验证
        Member member = new Member();
        member.setMemberID(memberID);
        List<Member> list2 = memberService.selectByStatement(member);
        if(list2==null || list2.size()<1){
            return new DataResponseVo<Object>(StatusCode.COMMON_PARAM_ERROR.getCode(),StatusCode.COMMON_PARAM_ERROR.getMessage());
        }
        //是否已经加入组织
        MemberOrg memberOrg =new MemberOrg();
        memberOrg.setOrgid(list1.get(0).getId());
        memberOrg.setMember(list2.get(0).getId());
        List<MemberOrg> list3 = memberOrgService.selectByStatement(memberOrg);
        if( list3.size()>0){
            return new DataResponseVo<Object>(StatusCode.VOLUNTEER_GROUP_REL_EXISTS.getCode(),StatusCode.VOLUNTEER_GROUP_REL_EXISTS.getMessage());
        }
        //加入组织待审核
        memberOrg.setStatus(DKQConstant.APPLY_STATUS);
        memberOrg.setUserType(DKQConstant.ROLES_VOLUNTEER);
        memberOrgService.insert(memberOrg);

        return new DataResponseVo<Object>(StatusCode.OK.getCode(), StatusCode.OK.getMessage());
    }

}
